#include "debug.h"
#include "record.h"
#include "ab_list.h"
#include "cbz_framework.h"

/************** Use when need to handle source data to get frame *************/
/// pre-process video pack to get video frame from source to record
int cbz_video_preprocess(struct record_frame_info *frame,unsigned char *source)
{
    if(frame == NULL || source == NULL)
    {
	SLOG(ERR,"invalid params!\n");
	return -1;
    }

    int pack_len = 0,ret = 0;
    //struct circle_buf_zone *vcbz = (struct circle_buf_zone *)source;
    //unsigned char *pdata = NULL;
    st_ab_dynamic_list *vcbz = (st_ab_dynamic_list *)source;

    frame->len = 0;
    frame->pts = 0;
    frame->index = -1;
    frame->flag = 0;
    
    while(1)
    {
#if 0
	pdata = get_data_from_cbz(vcbz, &pack_len);
	if(pdata==NULL)
	{/* if empty */
	    //ret = -1;
	    break;
	}
	else if(frame->len+pack_len <= frame->size)
	{
	    memcpy(frame->addr+frame->len,pdata,pack_len);
	    frame->flag = 1;
	    frame->len += pack_len;
	    del_cnt(vcbz);
	}
	else
	{
	    //SLOG(WRN,"pack len = %d!!\n",pack_len);
	    //ret = -1;
	    //del_cnt(vcbz);
	    break;
	}
#endif
	pack_len = ab_read_item_data(vcbz,frame->addr+frame->len,frame->size-frame->len);	
	//printf("+++++ab_read pack_len = %d\n",pack_len);
	if(pack_len < 0)
	{
	    //SLOG(ERR,"ab_read_item_data fail,ret = %d\n",ret);
	    ret = -1;
	    break;
	}
	else if(pack_len == 0)
	{
	    //SLOG(PRI,"ab list is empty!\n");
	    ret = 0;
	    break;
	}
	//else if(frame->index>=5)
	//{// control read rate
	//    ret = 0;
	//    break;
	//}
	else
	{
	    frame->flag = 1;
	    frame->len += pack_len;
	    frame->index++;
	}
    }

    return ret;
}

/// pre-process audio pack to get audio frame from source to record
///
/// To CBZ audio source,because it's same to video source packed,so the
/// preprocess callback also could use the video preprocess callback.
int cbz_audio_preprocess(struct record_frame_info *frame,unsigned char *source)
{
    frame->flag = 0;
    frame->len = 0;

    return 0;
}


/************** Use when local playback need *************/
/// post-process video frame read after demuxing from record file 
int cbz_video_postprocess(unsigned char *outpool,struct record_frame_info *frame)
{
    return 0;
}

/// post-process audio frame read after demuxing from record file
int cbz_audio_postprocess(unsigned char *outpool,struct record_frame_info *frame)
{
    return 0;
}
